#!/bin/python3
#coding=utf-8

from flask import Flask, render_template
import json
import flask


app = Flask(__name__)


@app.route('/')
def hello_world():
    users = {}
    with open('README.md', 'r') as f:
        for line in f.readlines():
            if "日期" in line:
                u = map(lambda x: x.strip(), line.split("|")[2:-1])
                for index, user in enumerate(list(u)):
                    users.setdefault(index, {'name': user, 'data': [0]})
            elif '2018' in line or '2019' in line:
                d = map(lambda x: x.strip(), line.split("|")[2:-1])
                for index, value in enumerate(list(d)):
                    last = users[index]['data'][-1]
                    if '-' != value:
                        users[index]['data'].append(eval(value)+last)
                    else:
                        users[index]['data'].append(last)
    content = render_template('index.html', legend=get_legend(users), xAxis=get_xAxis(users), series=get_series(users))
    return content.replace('&#39;', "'").replace('True', 'true')


def get_legend(datas):
    users = []
    for i in datas.values():
        users.append(i["name"])
    return users


def get_xAxis(datas):
    xAxis = []
    length = len(list(datas.values())[0]['data'])
    for i in range(1, length+1):
        xAxis.append(i)
    return json.dumps(xAxis)


def get_series(datas):
    series = []
    for i in datas.values():
        series.append({
            'name':i['name'],
            'type':'line',
            'smooth': True,
            'data':i['data']
        })
    return series

if __name__ == '__main__':
    app.run(host="0.0.0.0",port=80)

